package com.android.activity.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.R;
import com.android.activity.GoodsDetailsActivity;
import com.android.activity.adapter.StaggerAdapter;
import com.android.bean.Good;
import com.android.bean.User;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class GoodShowFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_TEXT = "param1";
    private static final String ARG_GOODS = "goodsTypeList";
    private ArrayList<Good> goodArrayList;
    private ArrayList<Good> typeGoodList;
    private RecyclerView recyclerView;
    private StaggerAdapter staggerAdapter;
    private User user;

    View rootView;

    private Integer mParam1;

    public GoodShowFragment() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static GoodShowFragment newInstance(Integer param1,List<Good> goodsList) {
        GoodShowFragment fragment = new GoodShowFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_TEXT, param1);
        args.putSerializable(ARG_GOODS, (Serializable) goodsList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 =  getArguments().getInt(ARG_TEXT);
            goodArrayList = (ArrayList<Good>) getArguments().getSerializable(ARG_GOODS);
        }
        if(goodArrayList != null) {
            typeGoodList = new ArrayList<>();
            for (int j = 0; j < goodArrayList.size(); j++) {
                if(goodArrayList.get(j).getTypeId() == mParam1){
                    typeGoodList.add(goodArrayList.get(j));
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if(rootView == null)
        rootView = inflater.inflate(R.layout.fragment_goods_type, container, false);
        initView();
        return rootView;

    }

    private void initView() {

        recyclerView = rootView.findViewById(R.id.rl_goods_show);
        if (goodArrayList != null){
            staggerAdapter = new StaggerAdapter(typeGoodList,getContext());
            //准备布局管理器
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            //设置布局管理器的方向
            layoutManager.setReverseLayout(false);
            //设置布局管理器到RecyclerView中
            recyclerView.setLayoutManager(layoutManager);
            //设置适配器
            recyclerView.setAdapter(staggerAdapter);
            //设置监听事件
            initListener();
        }


    }
    private void initListener() {
        staggerAdapter.setOnItemClickListener(new StaggerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //监听的各种操作写在这里

                Good goods = typeGoodList.get(position);
                //Eventbus发生事件
                EventBus.getDefault().postSticky(goods);
                //intent跳转
                Intent intent = new Intent(getActivity(), GoodsDetailsActivity.class);
                startActivity(intent);

            }
        });
    }

    @Override
    public void onStart() {


        super.onStart();


    }

    @Override
    public void onStop() {
        super.onStop();
        //EventBus解注册
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

}